package cn.wsh.org.controller;

import cn.wsh.basic.exception.BusinessException;
import cn.wsh.basic.util.ExcelUtil;
import cn.wsh.org.domain.ShopAuditLog;
import cn.wsh.org.service.IShopService;
import cn.wsh.org.domain.Shop;
import cn.wsh.org.query.ShopQuery;
import cn. wsh.basic.util.  AjaxResult;
import cn. wsh.basic.util.PageList;
import cn.wsh.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;



    /**
     * 店铺入驻    shop 传递的参数  店铺基本信息  + 店铺管理员的信息
     */
    @PostMapping("/settlement")
    public  AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return  AjaxResult.me();
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /*
     * 店铺人工审核 通过
     * */

    @PostMapping("/audit/pass")
    public  AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return  AjaxResult.me();
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }
    /*
     * 店铺人工审核 驳回
     * */

    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 导出所有店铺信息成excel
     * @param response
     */
    @GetMapping("/export/excel")
    public void export(HttpServletResponse response){
        List<Shop> shops = shopService.queryAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息", Shop.class,"Shop.xls",response);
    }

    /**
     * 导入店铺信息
     * @param file
     */
    @PostMapping("/import/excel")
    public void importExe(@RequestPart(value = "file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file,0,1, Shop.class);
        //使用mybatis的批量sql写入数据到数据库 @TODO
        System.out.println("导入数据一共【"+shops.size()+"】行");
    }

    /*
    * echarts 报表
    * */
    @GetMapping("/echarts")
    public List<Integer> echartsData(){
        List<ShopVo> shopVos=shopService.countByState();
        //把其中的数据转换为list或者数组  前端用于图表的y轴数据
        List<Integer> counts = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        return counts;
    }





    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public   AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return   AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public   AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return   AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return   AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
